﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BostonXNA.InputHandling.Input
{
	public class InputSequence : IInputSequence
	{
		protected SortedList<int, IInputActivity[]> items = new SortedList<int, IInputActivity[]>();

		protected int[] currentState = new int[] { 0, 0, 0, 0 };
		protected int lastIndex;

		public void Add( IInputActivity newItem )
		{
			items.Add( lastIndex++, new IInputActivity[] { newItem } );
		}

		public void Add( IInputActivity[] newItems )
		{
			items.Add( lastIndex++, newItems );
		}

		public bool Check( PlayerIndex player, InputState state )
		{
			bool sequenceComplete = false;
			bool stateComplete = true;

			foreach ( IInputActivity activity in items[ currentState[ (int)player ] ] )
			{
				stateComplete = stateComplete && activity.Check( state );
			}

			if ( stateComplete )
			{
				if ( currentState[ (int)player ] == lastIndex - 1 )
				{
					currentState[ (int)player ] = 0;
					sequenceComplete = true;
				}
				else
					currentState[ (int)player ]++;
			}
			return sequenceComplete;
		}

	}
}
